package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import com.itheima.reggie.utils.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private DishService dishService;

    /**
     * 新建套餐
     *
     * @param setmealDto
     * @return
     */
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> save(HttpServletRequest request, @RequestBody SetmealDto setmealDto) {
        log.info("套餐添加数据接收:{}", setmealDto);

        Long employee = (Long) request.getSession().getAttribute("employee");
        BaseContext.setCurrentId(employee);

        //保存套餐数据
        setmealService.save(setmealDto);

        return R.success("套餐创建成功!");
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        // 调service方法查询
        Page page1 = setmealService.page(page, pageSize, name);

        return R.success(page1);
    }

    /**
     * 套餐修改返回数据显示
     *
     * @param request
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R<SetmealDto> getSetmeal(HttpServletRequest request, @PathVariable Long id) {
        Long employee = (Long) request.getSession().getAttribute("employee");
        BaseContext.setCurrentId(employee);

        SetmealDto setmeal = setmealService.getSetmeal(id);

        return R.success(setmeal);
    }

    /**
     * 套餐修改
     *
     * @param request
     * @param setmealDto
     * @return
     */
    @PutMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> updateSetmeal(HttpServletRequest request, @RequestBody SetmealDto setmealDto) {

        Long employee = (Long) request.getSession().getAttribute("employee");
        BaseContext.setCurrentId(employee);

        //保存套餐数据
        setmealService.updateSetmeal(setmealDto);

        return R.success("套餐修改成功!");
    }

    /**
     * 套餐起售/停售修改
     *
     * @param sta
     * @param ids
     * @return
     */
    @PostMapping("status/{sta}")
    public R<String> status(HttpServletRequest request, @PathVariable int sta, @RequestParam List ids) {
        log.info("状态:{},id集合:{}", sta, ids);

        Long employee = (Long) request.getSession().getAttribute("employee");
        BaseContext.setCurrentId(employee);

        // 调service方法执行修改
        setmealService.status(sta, ids);

        return R.success("套餐状态修改成功!");
    }

    /**
     * 套餐删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)   // 删除缓存中指定名称的数据,allEntries = true表示删除该名称下所有的缓存数据
    public R<String> delete(@RequestParam List<Long> ids) {
        log.info("接收删除id:{}", ids);

        // 调用service中的删除方法
        setmealService.delete(ids);

        return R.success("删除成功!");
    }

    /**
     * 分类套餐查询(移动端)
     *
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache", key = "#setmeal.categoryId+'_'+#setmeal.status")     //若缓存中有数据,直接返回,没有则执行方法查询数据库,并将查询的数据添加到缓存一份再返回
    public R<List<Setmeal>> listSetmealDto(Setmeal setmeal) {
        log.info("套餐id:{},启用状态:{}", setmeal.getCategoryId(), setmeal.getStatus());
        // 根据分类套餐id,查询套餐分类下的所有套餐
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getCategoryId, setmeal.getCategoryId()).eq(Setmeal::getStatus, setmeal.getStatus());
        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }

    /**
     * 查询套餐下的菜品
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/dish/{categoryId}")
    public R<List<Dish>> getDish(@PathVariable Long categoryId) {
        log.info("套餐id:{}", categoryId);
        //根据套餐id,查询套餐下的菜品信息
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getCategoryId, categoryId);
        List<Dish> list = dishService.list(queryWrapper);

        return R.success(list);
    }
}
